package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.woniuxy.oldmanapi.entity.Food;
import com.woniuxy.oldmanapi.entity.Recipe;
import com.woniuxy.oldmanapi.entity.Recipecategory;
import com.woniuxy.oldmanapi.mapper.FoodMapper;
import com.woniuxy.oldmanapi.mapper.RecipeMapper;
import com.woniuxy.oldmanapi.mapper.RecipecategoryMapper;
import com.woniuxy.oldmanapi.yvo.RecipeVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecipeService {
    @Autowired
    RecipeMapper recipeMapper;
    @Autowired
    FoodMapper foodMapper;

    public PageInfo<RecipeVo> initSelectByCons(Integer goryId, String name, Integer now, Integer size) {
        PageHelper.startPage(now, size);
        List<RecipeVo> arr = recipeMapper.initSelectByCons(goryId, name);
        return new PageInfo<>(arr);
    }

    public Recipe selectRecipeById(Integer id) {
        return recipeMapper.selectByPrimaryKey(id);
    }

    public void updateRecipe(Recipe recipe) {
        recipeMapper.updateByPrimaryKeySelective(recipe);
    }

    public RecipeVo SelectRecipeVoByRecipeId(Integer recipeId) {
        return recipeMapper.SelectRecipeVoByRecipeId(recipeId);
    }

    public void addRecipe(RecipeVo recipe) {
        Recipe recipe1 = new Recipe();
        recipe1.setRecipeDetail(recipe.getRecipeDetail());
        recipe1.setRecipename(recipe.getRecipename());
        recipe1.setRecipebegintime(recipe.getRecipebegintime());
        recipe1.setRecipeendtime(recipe.getRecipeendtime());
        recipe1.setRecipecategoryId(recipe.getRecipecategoryId());
        recipe1.setRecipestatus(0);
        recipeMapper.insert(recipe1);
        List<Food> foodList = recipe.getFoodList();
        for (Food food :foodList){
            foodMapper.addFoodByRecipeId(food.getId(),recipe1.getId());
        }
    }

    public void modifyStatusById(Integer id) {
        foodMapper.modifyStatusById(id);
    }

    public void deleteRecipe(Integer recipeId) {
        recipeMapper.deleteByPrimaryKey(recipeId);
    }
}
